<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "fzs";

// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 查询每个部门的员工数量和性别统计
$sql = "SELECT d.name,COUNT(e.id) AS total_employees,SUM(CASE WHEN e.gender = '0' THEN 1 ELSE 0 END) AS male_count,SUM(CASE WHEN e.gender = '1' THEN 1 ELSE 0 END) AS female_count
        FROM 
            emp e
        LEFT JOIN 
            dept d ON e.dept_id = d.id
        GROUP BY 
            d.name";
$result = $conn->query($sql);

// 准备以JSON格式输出的数据
$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = [
        'name' => $row['name'],
        'totalEmployees' => (int)$row['total_employees'],
        'maleCount' => (int)$row['male_count'],
        'femaleCount' => (int)$row['female_count']
    ];
}

// 关闭数据库连接
$conn->close();

// 将数据编码为JSON格式，用于AJAX响应
header('Content-Type: application/json');
echo json_encode($data);
?>

